* MakePolicyPanel.do  05/30/23
* Make a state-year policy panel and collapse it to division-year

* (1) get state-level 2004-2009 OxyContin misuse rate and merge to region/fips xwalk
tempfile fioxy
use stfips year initial_oxy if year==2010 using APP2018, clear /* any year is fine */
keep stfips initial_oxy
sort stfips
save `fioxy', replace
use postal CensusDivisionCDC stfips using StateXwalk, clear
sort stfips
merge stfips using `fioxy'
tab _merge
drop _merge

* (2) merge with PDMP dates and expand to state-year panel
sort postal
merge postal using PDMPDates
tab _merge
drop _merge

* (3) merge with some population constants
sort postal
save `fioxy', replace
use StatePopulation if year>=2004 & year<=2010 & postal!="KR", clear  // 204K hurricane evacuees coded by SEER as postal=="KR"
gen pop20042009 = pop if year>=2004 & year<=2009
gen pop2010 = pop if year==2010
collapse (mean) pop20042009 pop2010, by(postal)
sort postal
merge postal using `fioxy'
tab _merge
drop _merge

* (4) expand to state-year panel
gen int year = 1999
gen byte tmpnyrs = 2021-year+1
expand tmpnyrs, gen(tmpfillin)
sort postal tmpfillin
by postal: replace year = year[_n-1]+1 if tmpfillin>0
drop tmp*

* (5) create have-policy indicators, with fractional if introduced after January
foreach var in PDMPany PDMPmust PDMPmustalt pillmill {
    gen have`var' = year > year(dofm(start`var'))
	replace have`var' = (13-month(dofm(start`var')))/12 if year == year(dofm(start`var'))
	replace have`var' = 0 if start`var'==.
}

gen byte apppost = year > 2010
label var havePDMPany "Fraction of year having any PDMP (Kim 2021)"
label var havePDMPmust "Fraction of year having must-access PDMP (Kim 2021)"
label var havePDMPmustalt "Fraction of year having must-access PDMP (Kim 2021, alternate)"
label var havepillmill "Fraction of year having pill mill law (Kim 2021)"
label var apppost "Post-reformulation indicator from Alpert-Powell-Pacula"
label var pop20042009 "Average state population 2004-2009"
label var pop2010 "State population 2010"
sort postal year

* *** Stop here for state X year ***

* (6) collapse to Census division X year
gen initial_oxypop = initial_oxy*pop20042009
foreach var in PDMPany PDMPmust PDMPmustalt pillmill {
    gen pop`var' = pop2010*have`var'
}
collapse (median) apppost (sum) pop20042009 pop2010 initial_oxypop popPDMPany popPDMPmust popPDMPmustalt poppillmill, by(CensusDivisionCDC year)
gen initial_oxy = initial_oxypop/pop20042009
foreach var in PDMPany PDMPmust PDMPmustalt pillmill {
    gen have`var' = pop`var'/pop2010
	drop pop`var'
}
drop initial_oxypop
label var initial_oxy "2004-2009 OxyContin misuse rate (APP 2018)"
label var havePDMPany "Fraction of person-years having any PDMP (Kim 2021)"
label var havePDMPmust "Fraction of person-years having must-access PDMP (Kim 2021)"
label var havePDMPmustalt "Fraction of person-years having must-access PDMP (Kim 2021, alternate)"
label var havepillmill "Fraction of person-years having pill mill law (Kim 2021)"
label var apppost "Post-reformulation indicator from Alpert-Powell-Pacula"
label var pop20042009 "Average Census division population 2004-2009"
label var pop2010 "Census division population 2010"
sort CensusDivisionCDC year

label data "PDMP, Pill mill, and Oxy-reformulation policies: Census division by year"
save PolicyPanel
